package com.taotao.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
@Aspect
@Slf4j
public class ExtAsyncAop {

    private ExecutorService executorService;

    public ExtAsyncAop() {
        executorService = Executors.newFixedThreadPool(10);
    }

    // 只要方法上用注解，可以直接拿到方法，不需要再判断，减少代码冗余
    // 获取到方法上有加上ExtAsync直接进入环绕通知
    @Around(value = "@annotation(com.taotao.proxy01.ext.TaoTaoAsync)")
    public void doBefore(ProceedingJoinPoint joinPoint) throws Exception {
        log.info(">>>拦截到方法上有加上异步注解ExtAsync");
        executorService.execute(() -> {
            //执行目标方法
            try {
                joinPoint.proceed();
            } catch (Throwable throwable) {
                log.error("e:{}", throwable);
            }
        });
    }
}